﻿using System.Collections.Generic;
using System.Web.Mvc;

namespace NetSurveillance.WebApp.Exts
{
    public static class ViewDataExtension
    {
        public static T Read<T>(this ViewDataDictionary viewdata, string name)
        {
            if (viewdata.ContainsKey(name))
                return (T)viewdata[name];
            return default(T);
        }

        public static IEnumerable<SelectListItem> ReadListItems(this ViewDataDictionary viewdata, string name)
        {
            if (viewdata.ContainsKey(name))
                return (IEnumerable<SelectListItem>)viewdata[name];
            return ViewHelper.EmptySelectList;
        }

        public static SelectList ReadSelectList(this ViewDataDictionary viewdata, string name, string selectedValue = null)
        {
            if (viewdata.ContainsKey(name))
                return string.IsNullOrEmpty(selectedValue)
                           ? (SelectList)viewdata[name]
                           : ViewHelper.SetSelectValue((SelectList)viewdata[name], selectedValue);
            return new SelectList(ViewHelper.EmptySelectList);
        }
    }
}